<?php

class Session_Adapter_Mongo extends Session_Adapter {

	/**
	 * Read session data
	 *
	 * @param string $id
	 * @return string
	 */
	public function read($id) {

		$item = Document::factory('core/session', $id);

		if ($item -> load() == false) {
			return '';
		}

		if ($item -> expired_at < time()) {
			return '';
		}

		return $item -> data;
	}

	/**
	 * Write session data
	 *
	 * @param string $id
	 * @param string $data
	 * @return boolean
	 */
	public function write($id, $data) {
		$item = Mongo_Document::factory('core/session', $id);

		$time = time();

		if ($item -> load() == false) {
			$item -> _id = $id;
			$item -> lifetime = $this -> getLifetime();
			$item -> expired_at = $this -> lifetime + time();
			$item -> modified_at = time();
		} else {
			$item -> modified_at = time();
			$item -> expired_at = time() + $item -> lifetime;
		}

		// becare of this.

		$item -> data = (string)$data;

		$item -> save();

		return true;
	}

	/**
	 * Destroy session
	 *
	 * @param string $id
	 * @return boolean
	 */
	public function destroy($id) {
		// Mongo_Document::factory('core/session') -> collection() -> remove(array('_id' => $id));

		$return = false;
	}

	/**
	 * Garbage Collection
	 *
	 * @param int $maxlifetime
	 * @return true
	 */
	public function gc($maxlifetime) {
		// Mongo_Document::factory('core/session') -> collection() -> delete(array('$'));

		return true;
	}

}
